//
//  KHBlueToothHelper.m
//  KissHealth
//
//  Created by bingo on 2019/1/8.
//  Copyright © 2019 Mark. All rights reserved.
//

#import "KHBlueToothHelper.h"

@implementation KHBlueToothHelper


+ (NSString *)getCurrentDateTime {
    NSDate *currentDate = [NSDate date];//获取当前时间，日期
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];// 创建一个时间格式化对象
    [dateFormatter setDateFormat:@"YYYY-MM-dd hh:mm:ss"];//设定时间格式,这里可以设置成自己需要的格式
    NSString *dateString = [dateFormatter stringFromDate:currentDate];//将时间转化成字符串
    return dateString;
}

+ (KHPeripheralType)peripheralTypeForMeter:(NSString *)meterString {
    if ([meterString isEqualToString:kBodyFatIdentifier]) {
        return KHPeripheralTypeBodyFat;
    } else if ([meterString isEqualToString:kBloodPressureIdentifier]) {
        return KHPeripheralTypeBloodPressure;
    } else if ([meterString isEqualToString:kBloodGlucoseIdentifier]) {
        return KHPeripheralTypeBloodGlucose;
    } else if ([meterString isEqualToString:kCholIdentifier]) {
        return KHPeripheralTypeChol;
    } else {
        return 0;
    }
}

+ (NSString *)meterStringForPeripheralType:(KHPeripheralType)type {
    if (type == KHPeripheralTypeBodyFat) return kBodyFatIdentifier;
    if (type == KHPeripheralTypeBloodPressure) return kBloodPressureIdentifier;
    if (type == KHPeripheralTypeBloodGlucose) return kBloodGlucoseIdentifier;
    if (type == KHPeripheralTypeChol) return kCholIdentifier;
    
    return @"";
}

#pragma mark - transfer tool
/// 将16进制data转换成字符串
+ (NSString *)hexStringWithData:(NSData *)data {
    const unsigned char* dataBuffer = (const unsigned char*)[data bytes];
    if(!dataBuffer) {
        return nil;
    }
    NSUInteger dataLength = [data length];
    NSMutableString* hexString = [NSMutableString stringWithCapacity:(dataLength * 2)];
    for(int i = 0; i < dataLength; i++) {
        [hexString appendString:[NSString stringWithFormat:@"%02lx", (unsigned long)dataBuffer[i]]];
    }
    NSString* result = [NSString stringWithString:hexString];
    return result;
    
}

/// 将16进制string转换成int
+ (NSInteger)numberWithHexString:(NSString *)hexString {
    
    const char *hexChar = [hexString cStringUsingEncoding:NSUTF8StringEncoding];
    
    int hexNumber;
    
    sscanf(hexChar, "%x", &hexNumber);
    
    return (NSInteger)hexNumber;
}

/// 将字符串转换成16进制 data
+ (NSData*)stringToByte:(NSString*)string {
    NSString *hexString=[[string uppercaseString] stringByReplacingOccurrencesOfString:@" " withString:@""];
    if ([hexString length]%2!=0) {
        return nil;
    }
    Byte tempbyt[1]={0};
    NSMutableData* bytes=[NSMutableData data];
    for(int i=0;i<[hexString length];i++)
    {
        unichar hex_char1 = [hexString characterAtIndex:i]; ////两位16进制数中的第一位(高位*16)
        int int_ch1;
        if(hex_char1 >= '0' && hex_char1 <='9')
            int_ch1 = (hex_char1-48)*16;   //// 0 的Ascll - 48
        else if(hex_char1 >= 'A' && hex_char1 <='F')
            int_ch1 = (hex_char1-55)*16; //// A 的Ascll - 65
        else
            return nil;
        i++;
        
        unichar hex_char2 = [hexString characterAtIndex:i]; ///两位16进制数中的第二位(低位)
        int int_ch2;
        if(hex_char2 >= '0' && hex_char2 <='9')
            int_ch2 = (hex_char2-48); //// 0 的Ascll - 48
        else if(hex_char2 >= 'A' && hex_char2 <='F')
            int_ch2 = hex_char2-55; //// A 的Ascll - 65
        else
            return nil;
        
        tempbyt[0] = int_ch1+int_ch2;  ///将转化后的数放入Byte数组里
        [bytes appendBytes:tempbyt length:1];
    }
    return bytes;
}

#pragma mark - 打开蓝牙弹窗
+ (void)showOpenBlueToothAlert {
    NSString *prompt = @"连接到配件需要您打开蓝牙";
    NSString *actionTitle = @"打开蓝牙";
    
    UIViewController *fromViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    
    UIAlertController *alert = [UIAlertController
                                alertControllerWithTitle:nil
                                message:prompt
                                preferredStyle:UIAlertControllerStyleAlert];
    
    //Add Buttons
    UIAlertAction *yesButton = [UIAlertAction
                                actionWithTitle:actionTitle
                                style:UIAlertActionStyleDefault
                                handler:^(UIAlertAction * action) {
                                    //Handle your ok please button action here
                                    [alert dismissViewControllerAnimated:YES completion:nil];
                                    
                                    // 跳转到设置 - 蓝牙
                                    NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
                                    if ([[UIApplication sharedApplication] canOpenURL:url]){
                                        [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
                                    }
                                }];
    
    UIAlertAction *cancelButton = [UIAlertAction
                                   actionWithTitle:@"取消"
                                   style:UIAlertActionStyleCancel
                                   handler:^(UIAlertAction * action) {
                                       //Handle your ok please button action here
                                       [alert dismissViewControllerAnimated:YES completion:nil];
                                   }];
    
    //Add your buttons to alert controller
    [alert addAction:yesButton];
    [alert addAction:cancelButton];
    [fromViewController presentViewController:alert animated:YES completion:nil];
}



@end
